package psn.kiko.config.mybatis.plugin.executor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

/**
 * mybatis查询监测拦截器
 */
@Component
@Slf4j
@Intercepts(@Signature(
		type=Executor.class,
		method="query",
		args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class,CacheKey.class,
				BoundSql.class}
))
public class ExecutorQueryPlugin implements Interceptor{
	@Override
	public Object intercept(Invocation invocation) throws Throwable{
		log.info("---------->SQL查询插件开始运行");
		Object[] args = invocation.getArgs();
		log.debug("SQL入参列表：{}",invocation.getArgs()[1]);
		long cur=System.currentTimeMillis();
		Object r=invocation.proceed();
		log.debug("SQL执行耗时：{} ms",System.currentTimeMillis()-cur);
		log.info("<----------SQL查询插件结束运行");
		return r;
	}
}
